package ai.fabu.dataplatform.config;

import ai.fabu.dataplatform.bean.bagdata.BagData;
import ai.fabu.dataplatform.bean.framedata.FrameData;
import ai.fabu.dataplatform.bean.groupdata.GroupData;
import ai.fabu.dataplatform.bean.groupdata.MapNameData;
import ai.fabu.dataplatform.bean.groupdata.VehicleNameData;
import ai.fabu.dataplatform.bean.task.GroupDataTask;
import ai.fabu.dataplatform.bean.topicdata.TopicData;
import ai.fabu.dataplatform.bean.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


@Slf4j
@Component
public class MongodbInit {

    @Autowired
    MongoOperations mongoOperations;

    @PostConstruct
    public void createIndexInit(){
        log.info("createIndexInit: ");
        if(!mongoOperations.collectionExists(GroupData.class)){
            mongoOperations.createCollection(GroupData.class);
        }
        if(!mongoOperations.collectionExists(BagData.class)){
            mongoOperations.createCollection(BagData.class);
        }
        if(!mongoOperations.collectionExists(TopicData.class)){
            mongoOperations.createCollection(TopicData.class);
        }
        if(!mongoOperations.collectionExists(FrameData.class)){
            mongoOperations.createCollection(FrameData.class);
        }
        if(!mongoOperations.collectionExists(GroupDataTask.class)){
            mongoOperations.createCollection(GroupDataTask.class);
        }
        if(!mongoOperations.collectionExists(MapNameData.class)){
            mongoOperations.createCollection(MapNameData.class);
        }
        if(!mongoOperations.collectionExists(VehicleNameData.class)){
            mongoOperations.createCollection(VehicleNameData.class);
        }
        if(!mongoOperations.collectionExists(User.class)){
            mongoOperations.createCollection(User.class);
        }
    }

//    @Order(4)
//    @Bean
//    public void completingAndFailTaskTableInit(){
//        log.info("completingAndFailTaskTableInit: ");
//        Query query = new Query();
//        query.addCriteria(Criteria.where(COMPLETE_STATUS).is(PROCESS_FAILURE_STATUS));
//        query.fields().include(GROUP_NAME);
//        List<GroupDataTask> groupDataTaskList = mongoOperations.find(query, GroupDataTask.class);
//        groupDataTaskList.forEach(groupDataTask -> TaskStatusTable.taskList.add(groupDataTask.getGroupName()));
//    }

}
